package com.oristand.controller;

import com.oristand.pojo.Manufacturer;
import com.oristand.service.ManufacturerService;
import com.oristand.util.ResultResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author dingweien
 * @version 1.0
 * @Date 2021/11/29 15:37
 */
@RestController
@CrossOrigin
public class ManufacturerController {


    private ManufacturerService manufacturerService;

    @Autowired
    public void setManufacturerService(ManufacturerService manufacturerService) {
        this.manufacturerService = manufacturerService;
    }

    /**
     * 查询全部厂商
     * 测试地址 /manufacturerList
     * @return resultResponse
     */
    @RequestMapping(path = "/manufacturerList",method = RequestMethod.GET)
    public ResultResponse getManufacturerList() {
        ResultResponse<List<Manufacturer>> resultResponse = new ResultResponse<>();
        List<Manufacturer> manufacturersList = manufacturerService.findManufacturerList();
        if (manufacturersList != null){
            resultResponse.selectSuccess(manufacturersList);
        } else {
            resultResponse.selectFail(ResultResponse.SELECT_FAIL);
        }
        return resultResponse;
    }


    /**
     * 根据主键ID查询厂商
     * 测试地址 /findByManufacturerId/1
     * @param manufacturerId 厂商id
     * @return 返回resultResponse
     */
    @RequestMapping(path = "/findByManufacturerId/{manufacturerId}", method = RequestMethod.GET)
    public ResultResponse findByManufacturerId(@PathVariable("manufacturerId") Integer manufacturerId) {
        ResultResponse<Manufacturer> resultResponse = new ResultResponse<>();
        Manufacturer manufacturer = manufacturerService.findByManufacturerId(manufacturerId);
        if (manufacturer != null) {
            resultResponse.selectSuccess(manufacturer);
        } else {
            resultResponse.selectFail(null);
        }
        return resultResponse;
    }

    /**
     * 根据主键ID删除厂商
     * 测试地址 /deleteByManufacturerId/1
     * @param manufactureerId 厂商id
     * @return 返回resultResponse
     */
    @RequestMapping(path = "/deleteByManufacturerId/{manufacturerId}", method = RequestMethod.DELETE)
    public ResultResponse deleteManuFacturerById(@PathVariable("manufacturerId") Integer manufactureerId) {
        ResultResponse<Integer> resultResponse = new ResultResponse<>();
        int row = manufacturerService.deleteManufacturerById(manufactureerId);
        if (row > 0) {
            resultResponse.deleteSuccess(row);
        } else {
            resultResponse.deleteFail(ResultResponse.DELETE_FAIL);
        }
        return resultResponse;
    }

    /**
     * 插入新厂商信息
     * @param manufacturer 厂商
     * @return 返回resultResponse
     */
    @RequestMapping(path = "/saveManufacturer", method = RequestMethod.PUT)
    public ResultResponse insertManufacturer(@RequestBody Manufacturer manufacturer) {
        ResultResponse<Integer> resultResponse = new ResultResponse<>();
        manufacturer.setStatus(1);
        int row = manufacturerService.insertManufacturer(manufacturer);
        if (row > 0) {
            resultResponse.insertSuccess(row);
        } else {
            resultResponse.insertFail(ResultResponse.INSERT_FAIL);
        }
        return resultResponse;
    }


    /**
     * 更改厂商信息
     * @param manufacturer 厂商对象
     * @return 返回resultResponse
     */
    @RequestMapping(path = "/updateManufacturer", method = RequestMethod.POST)
    public ResultResponse updateManufacturerById(@RequestBody Manufacturer manufacturer) {
        int row = manufacturerService.updateManufacturerById(manufacturer);
        ResultResponse<Integer> resultResponse = new ResultResponse<>();
        if (row > 0) {
            resultResponse.updateSuccess(row);
        } else {
            resultResponse.updateFail(ResultResponse.UPDATE_FAIL);
        }
        return resultResponse;
    }
}
